HackerRank Weighted Uniform Strings
提出
8/32 test cases failed :(
code: python
import math
import os
import random
import re
import sys
import string
#
# Complete the 'weightedUniformStrings' function below.
#
# The function is expected to return a STRING_ARRAY.
# The function accepts following parameters:
# 1. STRING s
# 2. INTEGER_ARRAY queries
#
def weightedUniformStrings(s, queries):
# Write your code here
for i, char in enumerate(list(string.ascii_lowercase)):
sums = set()
for i in range(26):
sums.add((i+1)* (j+1))
results = []
for i in queries:
if i in sums:
results.append("Yes")
else:
results.append("No")
return results
if __name__ == '__main__':
s = input()
queries_count = int(input().strip())
queries = []
for _ in range(queries_count):
queries_item = int(input().strip())
queries.append(queries_item)
result = weightedUniformStrings(s, queries)
fptr.write('\n'.join(result))
fptr.write('\n')
fptr.close()
解答
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'weightedUniformStrings' function below.
#
# The function is expected to return a STRING_ARRAY.
# The function accepts following parameters:
# 1. STRING s
# 2. INTEGER_ARRAY queries
#
def weightedUniformStrings(s, queries):
# Write your code here
result = []
# to store the weights present in the string
d = {}
weight = 0
# main logic
for i in range(len(s)):
# if adjacent char are different
weight = ord(si) - ord('a') + 1 else:
weight = weight + ord(si) - ord('a') + 1 # store the weight
# result
for q in queries:
result.append("Yes" if q in d else "No")
return result
if __name__ == '__main__':
s = input()
queries_count = int(input().strip())
queries = []
for _ in range(queries_count):
queries_item = int(input().strip())
queries.append(queries_item)
result = weightedUniformStrings(s, queries)
fptr.write('\n'.join(result))
fptr.write('\n')
fptr.close()
テーマ
メモ
https://www.youtube.com/watch?v=xn8f_EHuAWk
提出
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'weightedUniformStrings' function below.
#
# The function is expected to return a STRING_ARRAY.
# The function accepts following parameters:
# 1. STRING s
# 2. INTEGER_ARRAY queries
#
# abccddde
def weightedUniformStrings(s, queries):
# Write your code here
d = {}
for i in range(97, 123):
res = []
for i in range(1, len(s)):
res.append(res-1 + d[si]) else:
res = set(res)
ans = []
for q in queries:
if q in res:
ans.append("Yes")
else:
ans.append("No")
return ans
if __name__ == '__main__':
s = input()
queries_count = int(input().strip())
queries = []
for _ in range(queries_count):
queries_item = int(input().strip())
queries.append(queries_item)
result = weightedUniformStrings(s, queries)
fptr.write('\n'.join(result))
fptr.write('\n')
fptr.close()